package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.DishInSetMealStartException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 菜品服务层接口实现类
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 新增菜品 + 新增此菜品口味
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveDishAndFlavors(DishDTO dishDTO) {

        //新增菜品表内容
        Dish dish = new Dish();
        //属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        //调用新增菜品方法
        dishMapper.saveDish(dish);
        //新增口味之前需要先获取 DishId 通过主键回显获取
        Long id = dish.getId();

        //新增口味表内容
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        //判断是否有口味
        if(dishFlavors != null && !dishFlavors.isEmpty()){

            //将DishId注入到口味表中
            for(DishFlavor flavor : dishFlavors){
                //对口味设置菜品ID
                flavor.setDishId(id);
            }
            //将口味批量的插入数据库表中
            dishFlavorMapper.insertDishFlavorBatch(dishFlavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //通过插件设置插件需要的数据
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //查询数据库
        Page<DishVO> dishPage = dishMapper.pageQuery(dishPageQueryDTO);
        //返回数据库查询的数据
        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }

    /**
     * 通过菜品ID获取当前菜品的信息以及菜品的口味
     * @param id
     * @return
     */
    @Override
    public DishVO selectDishById(Long id) {

        //通过ID获取当前菜品信息
        Dish dish = dishMapper.selectDishById(id);

        //通过ID获取当前菜品的口味信息
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectDishFlavorByDishId(id);

        //组合 返回数据
        DishVO dishVO = new DishVO();
        //将菜品信息合并到 DishVO
        BeanUtils.copyProperties(dish,dishVO);
        //将菜品口味集合设置到 DishVO
        dishVO.setFlavors(dishFlavorList);
        //返回菜品
        return dishVO;
    }

    /**
     * 修改菜品信息以及菜品口味信息
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateDishAndDishFlavor(DishDTO dishDTO) {

        //通过获取的菜品内容修改菜品信息
        Dish dish = new Dish();
        //属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        //修改
        dishMapper.updateDish(dish);

        //通过获取的菜品口味集合进行批量修改菜品口味
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();

        //修改
        if(dishFlavors != null && !dishFlavors.isEmpty()){
            //循环遍历口味
            for (DishFlavor flavor : dishFlavors){
                //设置口味的菜品ID
                flavor.setDishId(dish.getId());
            }

            //删除此菜品提交过来的其他口味
            dishFlavorMapper.deleteDishFlavorBatch(dishFlavors);

            //修改
            dishFlavorMapper.updateDishFlavorBatch(dishFlavors);
        }

        //方法2 先删除全部的口味数据，再重新添加口味数据

    }

    /**
     * 通过 ID 删除菜品信息
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {

        //先判断此菜品中是否有启售中的菜品 -- 有则不可删除
        for (Long id : ids) {
            //通过菜品ID进行获取所有的菜品
            Dish dish = dishMapper.selectDishById(id);
            //判断菜品是否在启用状态
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                //启用状态则不可删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //次判断此菜品是否存在于套餐中 -- 有则不可删除
//            List<SetmealDish> setmealDishList = setMealDishMapper.selectSetMealByDishId(id);
//            if (setmealDishList != null){
//                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
//            }
        }

        //删除菜品表中的数据
        for (Long id : ids) {
            //通过菜品ID删除菜品
            dishMapper.deleteDishById(id);
            //删除口味表中的数据
            dishFlavorMapper.deleteDishFlavorByDishId(id);
        }

    }

    /**
     * 起售停售菜品
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //通过ID起售停售菜品
        Dish dish = new Dish();
        //设置菜品ID
        dish.setId(id);
        //设置菜品状态
        dish.setStatus(status);
        //如果此菜品在某个套餐内且套餐在起售中无法停售菜品
        if (StatusConstant.DISABLE.equals(status)){
            //通过ID查询套餐
            List<SetmealDish> setmealDishList = setMealDishMapper.selectSetMealByDishId(id);
            //判断当前套餐是否为空
            if (setmealDishList != null){
                //遍历集合
                for (SetmealDish setmealDish : setmealDishList) {
                    //通过套餐ID查询所有的套餐并查看套餐的状态，如果其中一个是起售则不可停售菜品
                    Setmeal setmeal = setMealMapper.selectSetMealById(setmealDish.getSetmealId());
                    //套餐不为空
                    if(setmeal != null){
                        //判断套餐状态是否在启用
                        if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
                            //启用中则抛出异常 当前菜品关联了启用套餐,不能禁用
                            throw new DishInSetMealStartException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL_ON_STAERT);
                        }
                    }
                }
            }
        }
        //更新菜品信息
        dishMapper.updateDish(dish);
    }

    /**
     * 通过分类获取菜品信息
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectDishByCategoryId(Long categoryId) {
        //通过分类ID获取当前分类下的菜品信息
        return dishMapper.selectDishByCategoryId(categoryId);
    }

    /**
     * 通过分类获取菜品详细信息
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> selectDishByCategoryIdGetVo(Long categoryId) {
        //通过当前的分类ID获取当前数据库中所有的此分类的菜品
        List<DishVO> dishVOList = dishMapper.selectDishByCategoryIdGetVo(categoryId);
        //通过当前的菜品信息获取每个菜品的口味
        if (dishVOList != null){
            //循环遍历
            for (DishVO dishVO : dishVOList) {
                //通过 dishId 获取当前菜品的所有口味
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectDishFlavorByDishId(dishVO.getId());
                //设置口味信息
                dishVO.setFlavors(dishFlavorList);
            }
        }
        //返回菜品详细信息
        return dishVOList;
    }
}
